﻿using System;
using System.Collections.Generic;
using System.Text;
using BaBaoFan.Modifier.Model;
using System.Data;
using System.Data.SqlClient;
using Dapper;

namespace BaBaoFan.Modifier.Data
{
    public class AlgorithmInfoRepository : IAlgorithmInfoRepository
    {
        private IDbConnection _conn;
        public AlgorithmInfoRepository(DbConnection conn)
        {
            _conn = conn.Connection;
        }

        public bool Add(AlgorithmInfoEntity entity)
        {
            string sql = @"insert into algorithm_mill_record (algorithm_id,mill_id,hashrate,mill_unit,weight,powerdissipation) values (@AlgorithmId,@MillId,@HashRate,@MillUnit,@Weight,@PowerDissipation)";
            return _conn.Execute(sql, entity) > 0;
        }

        public bool Delete(int[] ids)
        {
            string sql = "update algorithm_mill_record set isdel=1 where id in @ids";
            return _conn.Execute(sql, new { ids }) > 0;
        }

        public IEnumerable<AlgorithmInfo> GetAlgorithmInfos()
        {
            string sql = @"select 
                                    info.id as Id,
                                    a.`Algorithm`,
                                    m.`name` as MillName,
                                    m.mill_kind as MillKind,
                                    m.mill_price as MillPrice,
                                    info.hashrate as HashRate,
                                    info.mill_unit as MillUnit,
                                    info.weight,
                                    info.powerdissipation
                                    from algorithm_mill_record  as info
                                    inner JOIN coin_algorithm as a on a.id=info.algorithm_id
                                    inner JOIN millinfo as m on m.id=info.mill_id
                                    where info.isdel=0";
            return _conn.Query<AlgorithmInfo>(sql);
        }

        public IEnumerable<AlgorithmInfo> GetByAlgorithm(int algorithmId)
        {
            throw new NotImplementedException();
        }

        public AlgorithmInfoEntity GetById(int id)
        {
            string sql = @"select 
                                    info.id as Id,
                                    info.algorithm_id AlgorithmId,
                                    info.mill_id as MillId,
                                    info.hashrate as HashRate,
                                    info.mill_unit as MillUnit,
                                    info.weight as Weight,
                                    info.powerdissipation as PowerDissipation
                                    from algorithm_mill_record  as info
                                    where info.id=@id";
            return _conn.QuerySingleOrDefault<AlgorithmInfoEntity>(sql, new { id });
        }

        public IEnumerable<AlgorithmInfo> GetByMillId(int millId)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<AlgorithmInfo> Search(AlgorithmInfoCondition infoCondition)
        {
            string tatolCountSql = @"select count(0)
                                    from algorithm_mill_record  as info
                                    inner JOIN coin_algorithm as a on a.id=info.algorithm_id
                                    inner JOIN millinfo as m on m.id=info.mill_id
                                    where  info.isdel=0";
            string sql = @"select 
                                    info.id as Id,
                                    a.`Algorithm`,
                                    m.`name` as MillName,
                                    m.mill_kind as MillKind,
                                    m.mill_price as MillPrice,
                                    info.hashrate as HashRate,
                                    info.mill_unit as MillUnit,
                                    info.weight,
                                    info.powerdissipation
                                    from algorithm_mill_record  as info
                                    inner JOIN coin_algorithm as a on a.id=info.algorithm_id
                                    inner JOIN millinfo as m on m.id=info.mill_id
                                    where  info.isdel=0";
            if (!string.IsNullOrEmpty(infoCondition.Algorithm))
            {
                string addSql = " and a.`Algorithm` like CONCAT('%',@Algorithm,'%')";
                sql += addSql;
                tatolCountSql += addSql;
            }
            if (!string.IsNullOrEmpty(infoCondition.MillName))
            {
                string addSql = " and m.`name` like CONCAT('%',@MillName,'%')";
                sql += addSql;
                tatolCountSql += addSql;
            }
            infoCondition.TotalCount = _conn.QuerySingleOrDefault<int>(tatolCountSql, new { infoCondition.Algorithm, infoCondition.MillName});
            sql += " limit @PageIndex,@PageSize";
            return _conn.Query<AlgorithmInfo>(sql, new { infoCondition.Algorithm, infoCondition.MillName, PageIndex=(infoCondition.PageIndex-1)* infoCondition.PageSize, infoCondition.PageSize });
        }

        public bool Update(AlgorithmInfoEntity entity)
        {
            string sql = @"update algorithm_mill_record set algorithm_id=@AlgorithmId,mill_id=@MillId,hashrate=@HashRate,mill_unit=@MillUnit,weight=@Weight,powerdissipation=@PowerDissipation where id=@Id";
            return _conn.Execute(sql, entity) > 0;
        }
    }
}
